package cn.training.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.training.bean.UserBean;
import cn.training.service.HelloWorldService;


@Controller("HelloWorldController")
@RequestMapping("/")
public class HelloWorldController {

	@Autowired
	HelloWorldService helloWorldService;
	
 //   @RequestMapping(value = "/", method = RequestMethod.GET)
//    public String index(Model model) {
//        return "helloWorld";
//    }
	
	 @RequestMapping(value = "/", method = RequestMethod.GET)
   public String index(Model model) {
       return "index";
   }
	 
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="searchall")
    public String initLoginall(UserBean userBean, Model model) {
    	
    	UserBean result1 = helloWorldService.searchUser(userBean);
    	model.addAttribute("userBean", result1);
    	
    	if( result1 != null)
        {
    		List<UserBean> result = helloWorldService.searchallUser(userBean);
       	 model.addAttribute("list", result);
       	 return "show";
        }
        else
        {model.addAttribute("message", "您输入的用户名或密码错误，请重新输入！"); 
        
        }  	
    	 return "index";
    	
    	
  }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="zhuce")
    public String initZhuce(UserBean userBean,Model model) {
        return "zhuce";
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="submit")
    public String initSubmit(UserBean usersubmit, Model model) {
        String id=usersubmit.getUserId().toString();
        String name=usersubmit.getUserName().toString();
        String password=usersubmit.getUserPassword().toString();
  //      String password2=
        if(id==null||password==null)
        {  model.addAttribute("success", "注册失败，请重新填写！");
        	return "zhuce";
        }
   //     else if()
        else
        {int submit = helloWorldService.submit(usersubmit);
         model.addAttribute("success", "注册成功，请登录！");
         return "zhuce";}
    	
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="back")
    public String initBack(UserBean userBean,Model model) {
        return "index";
    }
    
    /*    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String initYes(UserBean userdelete,Model model) {
    	int delete = helloWorldService.delete(userdelete);
      	 model.addAttribute("list", delete);
      	 model.addAttribute("yes", "您输入^^^^^请重新输入！");
      	 return "show";
    }
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="no")
    public String initNo(UserBean userBean,Model model) {
    	List<UserBean> result = helloWorldService.searchallUser(userBean);
      	 model.addAttribute("list", result);
      	 return "show";
    }
    
   @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String initDelete(UserBean userdelete, Model model) {
    	
    	int delete = helloWorldService.delete(userdelete);
    	List<UserBean> result = helloWorldService.searchallUser(userdelete);
    	model.addAttribute("list", result);
    	return "confirm";
     //   return "show";
    }
 */   
    
    
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="search")
    public String initLogin(UserBean userBean, Model model) {
    	
    	UserBean result = helloWorldService.searchUser(userBean);
    	model.addAttribute("userBean", result);
        return "login";
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="insert")
    public String initInsert(UserBean userinsert, Model model) {
    	
    	int insert = helloWorldService.insert(userinsert);
        return "login";
    }
    
    @RequestMapping(value = "/init", method = RequestMethod.POST,params="update")
    public String initUpdate(UserBean userupdate, Model model) {
    	
    	int update = helloWorldService.update(userupdate);
        return "login";
    }
    
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public String initDelete(UserBean userdelete, Model model) {
    	
    	int delete = helloWorldService.delete(userdelete);
    	
    	
    	List<UserBean> result = helloWorldService.searchallUser(userdelete);
    	model.addAttribute("list", result);
    	return "confirm";
     //   return "show";
    }
    
}
